$TITLE  GTAPinGAMS -- Read the Multiregional Dataset

set i(*),r(*),f(*);
$gdxin ..\data\%dataset%.gdx
$load i r f

ALIAS (i,j), (r,s,rr);

SET     num(r)  Identifies the numeraire region;  num(r) = YES$(ORD(r) EQ 1);

*	These two sets are used in datasets aggregated from GTAP_DOE:

SET	annexb(r)	Identifies Annex B countries
	oecd(r)		Identifies OECD countries;

SET     cgd(i)  Identifies the investment good /cgd/;

SET     d       Submarkets for which import shares are differentiated / 
                G       Public, 
                C       Private Consumption,
                I       Intermediate input /;

PARAMETER
        ty(i,r)         Output tax
        ti(j,i,r)       Intermediate input tax
        tf(f,i,r)       Factor tax
        tx(i,s,r)       Export tax rate (defined on a net basis)
        tm(i,s,r)       Import tariff rate
        tg(i,r)         Tax rates on government demand
        tp(i,r)         Tax rate on private demand

        vafm(j,i,r)     Aggregate intermediate inputs
        vfm(f,i,r)      Value of factor inputs (net of tax)
        vxmd(i,r,s)     Value of commodity trade (fob - net export tax)
        vtwr(i,r,s)     Transport services
        vst(i,r)        Value of international transport sales
        vdgm(i,r)       Government demand (domestic)
        vigm(i,r)       Government demand (imported)
        vdpm(i,r)       Aggregate private demands (domestic)
        vipm(i,r)       Aggregate private demands (domestic)

*	These data are not used in GTAPinGAMS but are provide for
*	reference:

	vsave(r)	Regional savings
	vkb(r)		Value of capital stock (beginning of period)
	vdep(r)		Value of capital depreciation;

*       Use the libinclude routine if it is installed, otherwise look
*       in the inclib directory:

$load   ty ti tf tx tm tg tp vafm vfm vxmd vtwr vst vdgm vigm vdpm vipm vsave vdep vkb

*       Declare and assign intermediate variables:

PARAMETER
        vim(i,r)        Total value of imports (gross tariff)
        vxm(i,r)        Value of export (net excise tax)
        vdm(i,r)        Value of domestic output (net excise tax)
        vdfm(i,r)       Aggregate intermediate demand (domestic)
        vifm(i,r)       Aggregate intermediate demand (imported)

        vom(i,r)        Aggregate output value (gross of tax)
        vgm(i,r)        Public expenditures
        vpm(i,r)        Private expenditures
        vg(r)           Total value of public expenditure
        vp(r)           Total value of private expenditure
	vi(r)		Total value of investment
        vt              Value of trade markups
        vb(*)           Net capital inflows

        market(*,*)     Consistency check for calibrated benchmark

        evoa(f,r)       Value of factor income

        va(d,i,r)       Armington supply
        vd(d,i,r)       Domestic supply
        vm(d,i,r)       Imported supply;

vxm(i,r) =  sum(s, vxmd(i,r,s)) + vst(i,r);
vim(i,r) = sum(s,(vxmd(i,s,r)*(1+tx(i,s,r))+vtwr(i,s,r))*(1+tm(i,s,r)));
vdm(i,r) = ( sum(j, vafm(j,i,r)*(1+ti(j,i,r)))
           + sum(f,  vfm(f,i,r)*(1+tf(f,i,r)))) / (1-ty(i,r)) - vxm(i,r);
vdfm(i,r) = vdm(i,r) - vdgm(i,r) - vdpm(i,r) - vdm(i,r)$cgd(i);
vi(r) = sum(cgd, vdm(cgd,r));
vifm(i,r) = vim(i,r) - vipm(i,r) - vigm(i,r);

vom(i,r) = vdm(i,r) + vxm(i,r);
vgm(i,r) = vigm(i,r)+vdgm(i,r);
vpm(i,r) = vipm(i,r)+vdpm(i,r);
vg(r) = sum(i, vgm(i,r) * (1 + tg(i,r)));
vp(r) = sum(i, vpm(i,r) * (1 + tp(i,r)));
vt = sum((i,r), vst(i,r));

evoa(f,r) = sum(i, vfm(f,i,r));

vb(r) = vp(r) + vg(r) + vi(r)
        - sum(f, evoa(f,r)) 
        - sum(i,     ty(i,r)   * vom(i,r))
        - sum((i,j), ti(j,i,r) * vafm(j,i,r))
        - sum((i,f), tf(f,i,r) * vfm(f,i,r))
        - sum((i,s), tx(i,r,s)  * vxmd(i,r,s))
        - sum((i,s), tm(i,s,r)  * (vxmd(i,s,r)*(1+tx(i,s,r)) + vtwr(i,s,r)) )
        - sum(i,     tg(i,r)*vgm(i,r))
        - sum(i,     tp(i,r)*vpm(i,r));

vb("checksum") = sum(r, vb(r));
display vb;

vm("c",i,r) = vipm(i,r);        vd("c",i,r) = vdpm(i,r);
vm("g",i,r) = vigm(i,r);        vd("g",i,r) = vdgm(i,r);
vm("i",i,r) = vifm(i,r);        vd("i",i,r) = vdfm(i,r);
va(d,i,r) = vm(d,i,r) + vd(d,i,r);

*       Round these data to avoid spurious epsilons:

vxm(i,r)  = round( vxm(i,r), 6);
vim(i,r)  = round( vim(i,r), 6);
vdm(i,r)  = round( vdm(i,r), 6);
vdfm(i,r) = round( vdfm(i,r),6);
vifm(i,r) = round( vifm(i,r),6);
vom(i,r)  = round( vom(i,r), 6);
vgm(i,r)  = round( vgm(i,r), 6);
vpm(i,r)  = round( vpm(i,r), 6);
vg(r)     = round( vg(r),    6);
vp(r)     = round( vp(r),    6);
vt        = round( vt, 6);
evoa(f,r) = round( evoa(f,r),6);
vm(d,i,r) = round( vm(d,i,r),6);
vd(d,i,r) = round( vd(d,i,r),6);
va(d,i,r) = round( va(d,i,r),6);

*	Drop negligible trade flows:

vxmd(i,r,s) = round( vxmd(i,r,s), 8);
tx(i,r,s)$(vxmd(i,r,s) = 0) = 0;
vtwr(i,r,s)$(vxmd(i,r,s) = 0) = 0;
tm(i,r,s)$(vxmd(i,r,s) = 0) = 0;

*       Report on sign errors in the dataset:

if (smin((j,i,r), vafm(j,i,r)) lt 0, 
	vafm(j,i,r)$(vafm(j,i,r) lt 0) = round(vafm(j,i,r), 5);
	if (smin((j,i,r), vafm(j,i,r)) lt 0, 
	  vafm(j,i,r)$(vafm(j,i,r) gt 0) = 0;
          abort "Error: negative transactions in vafm:",vafm;);
	);
if (smin((f,i,r), vfm(f,i,r)) lt 0, 
	vfm(f,i,r)$(vfm(f,i,r) lt 0) = round(vfm(f,i,r), 5);
	if (smin((f,i,r), vfm(f,i,r)) lt 0, 
	   vfm(f,i,r)$(vfm(f,i,r) gt 0) = 0;
          abort "Error: negative transactions in vfm:",vfm;);
	);
if (smin((i,r,s), vxmd(i,r,s)) lt 0, 
	vxmd(i,r,s)$(vxmd(i,r,s) lt 0) = round(vxmd(i,r,s), 5);
	if (smin((i,r,s), vxmd(i,r,s)) lt 0, 
	  vxmd(i,r,s)$(vxmd(i,r,s) gt 0) = 0;
          abort "Error: negative transactions in vxmd:",vxmd;);
	);
if (smin((i,r,s), vtwr(i,r,s)) lt 0, 
	vtwr(i,r,s)$(vtwr(i,r,s) lt 0) = round(vtwr(i,r,s), 5);
	if (smin((i,r,s), vtwr(i,r,s)) lt 0, 
	  vtwr(i,r,s)$(vtwr(i,r,s) gt 0) = 0;
          abort "Error: negative transactions in vtwr:",vtwr;);
	);
if (smin((i,r), vst(i,r)) lt 0, 
	vst(i,r)$(vst(i,r) lt 0) = round(vst(i,r), 5);
	if (smin((i,r), vst(i,r)) lt 0, 
	       vst(i,r)$(vst(i,r) gt 0) = 0;
          abort "Error: negative transactions in vst:",vst;);
	);
if (smin((i,r), vdgm(i,r)) lt 0, 
	vdgm(i,r)$(vdgm(i,r) lt 0) = round(vdgm(i,r), 5);
	if (smin((i,r), vdgm(i,r)) lt 0, 
	      vdgm(i,r)$(vdgm(i,r) gt 0) = 0;
          abort "Error: negative transactions in vdgm:",vdgm;);
	);
if (smin((i,r), vigm(i,r)) lt 0, 
	vigm(i,r)$(vigm(i,r) lt 0) = round(vigm(i,r), 5);
	if (smin((i,r), vigm(i,r)) lt 0, 
	      vigm(i,r)$(vigm(i,r) gt 0) = 0;
          abort "Error: negative transactions in vigm:",vigm;);
	);
if (smin((i,r), vdpm(i,r)) lt 0, 
	vdpm(i,r)$(vdpm(i,r) lt 0) = round(vdpm(i,r), 5);
	if (smin((i,r), vdpm(i,r)) lt 0, 
	      vdpm(i,r)$(vdpm(i,r) gt 0) = 0;
          abort "Error: negative transactions in vdpm:",vdpm;);
	);
if (smin((i,r), vipm(i,r)) lt 0, 
	vipm(i,r)$(vipm(i,r) lt 0) = round(vipm(i,r), 5);
	if (smin((i,r), vipm(i,r)) lt 0, 
	      vipm(i,r)$(vipm(i,r) gt 0) = 0;
          abort "Error: negative transactions in vipm:",vipm;);
	);
if(smin((i,r), vdm(i,r)) lt 0, 
	vdm(i,r)$(vdm(i,r) lt 0) = round(vdm(i,r), 5);
	if(smin((i,r), vdm(i,r)) lt 0, 
	        vdm(i,r) = min(vdm(i,r), 0);
          abort "Error: negative values in imputed array vdm!",vdm;);
	);
if(smin((i,r), vdfm(i,r))  lt 0, 
	vdfm(i,r)$(vdfm(i,r) lt 0) = round(vdfm(i,r), 5);
	if(smin((i,r), vdfm(i,r))  lt 0, 
	      vdfm(i,r) = min(vdfm(i,r), 0);
          abort "Error: negative values in imputed array vdfm!",vdfm;);
	);
if (smin((i,r), vifm(i,r)) lt 0, 
	vifm(i,r)$(vifm(i,r) lt 0) = round(vifm(i,r), 5);
	if (smin((i,r), vifm(i,r)) lt 0, 
	      vifm(i,r) = min(vifm(i,r), 0);
          abort "Error: negative values in imputed array vifm!",vifm;);
	);

*       Generate market clearance check:

market(r,i) = vdfm(i,r) + vifm(i,r) - sum(j, vafm(i,j,r));
market("world","total") = vt - sum((i,r,s), vtwr(i,r,s));
market(r,i)$(round(market(r,i),5) eq 0) = 0;
DISPLAY market;

parameter
        pmx0(i,s,r)     Reference price of imports,
        pmt0(i,s,r)     Reference price of transport services,
        pg0(i,r)        Reference price of government demand,
        pc0(i,r)        Reference price for private demand,
        py0(i,r)        Reference output price (domestic or export),
        pai0(j,i,r)     Reference price for intermediate inputs,
        pf0(f,i,r)      Reference price for factor inputs;

pg0(i,r) = 1 + tg(i,r);
pc0(i,r) = 1 + tp(i,r);
pmt0(i,s,r) = (1 + tm(i,s,r));
pmx0(i,s,r) = (1 + tm(i,s,r)) * (1 + tx(i,s,r));
pai0(j,i,r) = 1 + ti(j,i,r);
py0(i,r) =1 - ty(i,r);
pf0(f,j,s) = 1 + tf(f,j,s);

*       Check the tax rates too:

if (smax((i,r), 1 - ty(i,r)) lt 0,   ty(i,r)$(ty(i,r) lt 1) = 0;
        abort "Error: output tax exceeds 100% on a gross basis!", ty;);
if (smax((i,r), 1 + tg(i,r)) le 0,   tg(i,r)$(1 + tg(i,r) gt 0) = 0;
        abort "Error: public demand subsidy exceeds 100% on a net basis!", tg;);
if (smax((i,r), 1 + tp(i,r)) le 0,   tp(i,r)$(1 + tp(i,r) gt 0) = 0;
        abort "Error: private demand subsidy exceeds 100% on a net basis!", tp;);
if (smin((j,i,r), 1 + ti(j,i,r)) lt 0, ti(j,i,r)$(ti(j,i,r) gt -1) = 0;
        abort "Error: intermediate input subsidy exceeds 100% on a net basis!",ti;);
if (smin((f,i,r), 1 + tf(f,i,r)) le 0, tf(f,i,r)$(tf(f,i,r) gt -1) = 0;
        abort "Error: factor input subsidy exceeds 100% on a net basis!",tf;);
if (smin((i,r,s), 1 + tx(i,r,s)) le 0, tx(i,r,s)$(tx(i,r,s) gt -1) = 0;
        abort "Error: export subsidy exceeds 100% on a net basis!",tx,vxmd;);
if (smin((i,r,s), 1 + tm(i,r,s)) le 0, tm(i,r,s)$(tm(i,r,s) gt -1) = 0;
        abort "Error: import subsidy exceeds 100% on a net basis!",tm;);
